package com.mytx.mapper;

import com.alibaba.fastjson.JSONObject;
import com.mytx.pojo.OrderReceiveInfo;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class OrderReceiveInfoHandler extends BaseTypeHandler<List<OrderReceiveInfo>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<OrderReceiveInfo> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSONObject.toJSONString(parameter));
    }

    @Override
    public List<OrderReceiveInfo> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        if (rs.getString(columnName) != null) {
            return JSONObject.parseArray(rs.getString(columnName)).toJavaList(OrderReceiveInfo.class);
        }
        return Collections.emptyList();
    }

    @Override
    public List<OrderReceiveInfo> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        if (rs.getString(columnIndex) != null) {
            return JSONObject.parseArray(rs.getString(columnIndex)).toJavaList(OrderReceiveInfo.class);
        }
        return Collections.emptyList();
    }

    @Override
    public List<OrderReceiveInfo> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        if (cs.getString(columnIndex) != null) {
            return JSONObject.parseArray(cs.getString(columnIndex)).toJavaList(OrderReceiveInfo.class);
        }
        return Collections.emptyList();
    }
}
